package com.example.hotelrear.entity;

import java.util.Random;

/**
 * 随机生成4位验证码
 */
public class CodeUtils {
    public static String getCode() {
        char codes[] = new char[52];
        for (int i = 0; i < codes.length; i++) {
            if (i < 26) {
                codes[i] = (char) ('a' + i);
            } else {
                codes[i] = (char) ('A' + i - 26);
            }
        }

        Random r = new Random();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(codes.length);
            if (index % 2 == 0) {
                sb.append(codes[index]);
            } else {
                sb.append(index % 10);
            }
        }
        return sb.toString();
    }

}
